package com.qzq.weibo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.Toast;

public class Utils {

	private static Utils utils;
	private static boolean isExit = false;
	private Context mContext;

	private final static int maxMemory = (int) Runtime.getRuntime().maxMemory();// 获取当前应用程序所分配的最大内存
	private final static int cacheSize = maxMemory / 5;// 只分5分之一用来做图片缓存

	public Utils(Context mContext) {
		this.mContext = mContext;
	}

	public static Utils getInstance(Context mContext) {
		if (utils == null) {
			utils = new Utils(mContext);
		}

		return utils;
	}

	/** 双击返回键退出程序 */
	public void exitBy2Click(Activity _Activity) {
		Timer tExit = null;
		if (!isExit) {
			isExit = true; // 准备退出
			Toast.makeText(_Activity.getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false; // 取消退出
				}
			}, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
		} else {
			_Activity.setResult(-1);
			_Activity.finish();
		}

	}

	/** Toast提示 */
	public void setToastMsg(String msg) {
		Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
	}

	public String setSimpleDateTime(String created_at) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);

		String time = null;
		String now = sdf.format(new Date());

		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数

		long diff;

		try {
			diff = sdf.parse(now).getTime() - sdf.parse(created_at).getTime();

			long day = diff / nd;// 计算差多少天
			long hour = diff % nd / nh;// 计算差多少小时
			long min = diff % nd % nh / nm;// 计算差多少分钟

			if (day == 0) {
				if (hour == 0) {
					if (min == 0) {
						time = "刚刚";
					} else {
						time = min + "分钟前";
					}

				} else {
					time = hour + "小时前";
				}
			} else {
				time = day + "天前";
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return time;
	}

	private static LruCache<String, Bitmap> mLruCache = new LruCache<String, Bitmap>(
			cacheSize) {
		protected int sizeOf(String key, Bitmap bitmap) {// 复写sizeof()方法
			return bitmap.getRowBytes() * bitmap.getHeight() / 1024; // 这里是按多少KB来算
		}
	};

	public void loadBitmap(String urlStr, ImageView image, int width, int height) {

		System.out.println(urlStr);
		AsyncImageLoader asyncLoader = new AsyncImageLoader(image, mLruCache,
				width, height);// 一个异步图片加载对象
		Bitmap bitmap = asyncLoader.getBitmapFromMemoryCache(urlStr);// 首先从内存缓存中获取图片
		if (bitmap != null) {
			image.setImageBitmap(bitmap);// 如果缓存中存在这张图片则直接设置给ImageView
		} else {
			// image.setImageResource(R.drawable.ic_codertool);// 否则先设置成默认的图片
			asyncLoader.execute(urlStr);// 然后执行异步任务AsycnTask 去网上加载图片
		}
	}
}
